<?php
/**
 * Includes the User_Model class as well as the required sub-classes
 * @package codeigniter.application.models
 */

/**
 * User_Model extends codeigniters base CI_Model to inherit all codeigniter magic!
 * @author Leon Revill
 * @package codeigniter.application.models
 */
class Customer_m extends CI_Model
{
	/*
	* A private variable to represent each column in the database
	*/
	private $nullValue = null;
	private $customer_code;	// 	 'customer_code' int(10) unsigned not null auto_increment,
	private $last_name;		//   'last_name' varchar(20) not null,
	private $first_name;	//   'first_name' varchar(20) not null,
	private $password;		//   'password' varchar(50) not null,
	private $customer_group_code = 'group1';//   'customer_group_code' varchar(16) not null,



//   'last_name_kana' varchar(40) default null,
//   'first_name_kana' varchar(40) default null,
	private $login_id = 'defaultID';//   'login_id' varchar(256) not null,
	private $email;			//   'email' varchar(256) default null,
	private $birth_date = 'now()';	//   'birth_date' datetime not null,
	private $sex = '1';		//   'sex' tinyint(3) unsigned not null,
//   'request_mail_type' tinyint(3) unsigned default null,
//   'client_mail_type' tinyint(3) unsigned default null,
//   'caution' varchar(200) default null,
//   'login_datetime' datetime default null,
//   'login_error_count' tinyint(3) unsigned default null,
//   'login_locked_flg' tinyint(3) unsigned default null,
//   'customer_status' tinyint(3) unsigned default null,
//   'customer_attribute_reply_date' datetime default null,
//   'latest_point_acquired_date' datetime default null,
//   'rest_point' int(10) unsigned default null,
//   'temporary_point' int(10) unsigned default null,
//   'withdrawal_request_date' datetime default null,
//   'withdrawal_date' datetime default null,
//   'orm_rowid' double default null,
//   'created_user' varchar(100) default null,
	private $created_datetime = 'now()';//   'created_datetime' datetime not null,
//   'updated_user' varchar(100) default null,
	private $updated_datetime = 'now()';//   'updated_datetime' datetime not null,
//   PRIMARY KEY ('CUSTOMER_CODE')

	function __construct()
	{
		parent::__construct();
	}

	/*
	* SET's & GET's
	* Set's and get's allow you to retrieve or set a private variable on an object
	*/


	/**
		ID
	**/

	/**
	* @return int [$this->customer_code] Return this objects ID
	*/
	public function getCustomer_code()
	{
		return $this->customer_code;
	}

	/**
	* @param int Integer to set this objects ID to
	*/
	public function setCustomer_code($value)
	{
		$this->customer_code = $value;
	}

	/**
		last_name
	**/

	/**
	* @return string [$this->last_name] Return this objects last_name
	*/
	public function getLast_name()
	{
		return $this->last_name;
	}

	/**
	* @param string String to set this objects last_name to
	*/
	public function setLast_name($value)
	{
		$this->last_name = $value;
	}

	/**
	 * @return string [$this->first_name] Return this objects first_name
	 */
	public function getFirst_name()
	{
		return $this->first_name;
	}

	/**
	 * @param string String to set this objects first_name to
	 */
	public function setFirst_name($value)
	{
		$this->first_name = $value;
	}

	/**
		PASSWORD
	**/

	/**
	* @return string [$this->password] Return this objects password
	*/
	public function getPassword()
	{
		return $this->password;
	}

	/**
	* @param string String to set this objects password to
	*/
	public function setPassword($value)
	{
		$this->password = $value;
	}
	/**
	 Email
	 **/

	/**
	 * @return string [$this->password] Return this objects email
	 */
	public function getEmail()
	{
		return $this->email;
	}

	/**
	 * @param string String to set this objects email to
	 */
	public function setEmail($value)
	{
		$this->email = $value;
	}
	/*
	* Class Methods
	*/

	/**
	*	Commit method, this will comment the entire object to the database
	*/
	public function commit()
	{
		$data = array(
// 			'last_name' => $this->last_name,
// 			'password' => $this->password

				'CUSTOMER_CODE'  => $this->customer_code,
				'CUSTOMER_GROUP_CODE'  => $this->customer_group_code,
				'LAST_NAME'  => $this->last_name,
				'FIRST_NAME' => $this->first_name,
				'LAST_NAME_KANA' => $this->nullValue,
				'FIRST_NAME_KANA' => $this->nullValue,
				'LOGIN_ID' => $this->nullValue,
				'EMAIL' => $this->email,
				'PASSWORD' => $this->password,
				'BIRTH_DATE' => $this->nullValue,
				'SEX' => $this->sex,
				'REQUEST_MAIL_TYPE' => $this->nullValue,
				'CLIENT_MAIL_TYPE' => $this->nullValue,
				'CAUTION' => $this->nullValue,
				'LOGIN_DATETIME' => $this->nullValue,
				'LOGIN_ERROR_COUNT' => $this->nullValue,
				'LOGIN_LOCKED_FLG' => $this->nullValue,
				'CUSTOMER_STATUS' => $this->nullValue,
				'CUSTOMER_ATTRIBUTE_REPLY_DATE' => $this->nullValue,
				'LATEST_POINT_ACQUIRED_DATE'=> $this->nullValue,
				'REST_POINT' => $this->nullValue,
				'TEMPORARY_POINT' => $this->nullValue,
				'WITHDRAWAL_REQUEST_DATE' => $this->nullValue,
				'WITHDRAWAL_DATE' => $this->nullValue,
				'ORM_ROWID' => $this->nullValue,
				'CREATED_USER' => $this->nullValue,
				'CREATED_DATETIME' => $this->created_datetime,
				'UPDATED_USER' => $this->nullValue,
				'UPDATED_DATETIME' => $this->updated_datetime

		);

//INSERT INTO zswebshop.customer
// (
//     customer_code,
//     customer_group_code,
//     last_name,
//     first_name,
//     last_name_kana,
//     first_name_kana,
//     login_id,
//     email,
//     password,
//     birth_date,
//     sex,
//     request_mail_type,
//     client_mail_type,
//     caution,
//     login_datetime,
//     login_error_count,
//     login_locked_flg,
//     customer_status,
//     customer_attribute_reply_date,
//     latest_point_acquired_date,
//     rest_point,
//     temporary_point,
//     withdrawal_request_date,
//     withdrawal_date,
//     orm_rowid,
//     created_user,
//     created_datetime,
//     updated_user,
//     updated_datetime
// )
// VALUES (
// )
		if ($this->customer_code > 0) {
			//We have an ID so we need to update this object because it is not new
			if ($this->db->update("customer", $data, array("customer_code" => $this->customer_code))) {
				return true;
			}
		} else {
			//We dont have an ID meaning it is new and not yet in the database so we need to do an insert
			if ($this->db->insert("customer", $data)) {
				//Now we can get the ID and update the newly created object
				$this->customer_code = $this->db->insert_id();
				return true;
			}
		}
		return false;
	}


	public function insert_customer($post)
	{
		$data = array(
		// 			'last_name' => $this->last_name,
		// 			'password' => $this->password

				'CUSTOMER_CODE'  => $this->customer_code,
				'CUSTOMER_GROUP_CODE'  => $this->customer_group_code,
				'LAST_NAME'  => $post['last_name'],//$this->last_name,
				'FIRST_NAME' => $post['first_name'],//$this->first_name,
				'LAST_NAME_KANA' => $this->nullValue,
				'FIRST_NAME_KANA' => $this->nullValue,
				'LOGIN_ID' => $post['email'],
				'EMAIL' => $post['email'],//$this->email,
				'PASSWORD' => $post['password'],//$this->password,
				'BIRTH_DATE' => '20001122',//$this->nullValue,
				'SEX' => $this->sex,
				'REQUEST_MAIL_TYPE' => $this->nullValue,
				'CLIENT_MAIL_TYPE' => $this->nullValue,
				'CAUTION' => $this->nullValue,
				'LOGIN_DATETIME' => $this->nullValue,
				'LOGIN_ERROR_COUNT' => $this->nullValue,
				'LOGIN_LOCKED_FLG' => $this->nullValue,
				'CUSTOMER_STATUS' => $this->nullValue,
				'CUSTOMER_ATTRIBUTE_REPLY_DATE' => $this->nullValue,
				'LATEST_POINT_ACQUIRED_DATE'=> $this->nullValue,
				'REST_POINT' => $this->nullValue,
				'TEMPORARY_POINT' => $this->nullValue,
				'WITHDRAWAL_REQUEST_DATE' => $this->nullValue,
				'WITHDRAWAL_DATE' => $this->nullValue,
				'ORM_ROWID' => $this->nullValue,
				'CREATED_USER' => $this->nullValue,
				'CREATED_DATETIME' => $this->created_datetime,
				'UPDATED_USER' => $this->nullValue,
				'UPDATED_DATETIME' => $this->updated_datetime

		);

		if ($this->customer_code > 0) {
			//We have an ID so we need to update this object because it is not new
			if ($this->db->update("customer", $data, array("customer_code" => $this->customer_code))) {
				return true;
			}
		} else {
			//We dont have an ID meaning it is new and not yet in the database so we need to do an insert
			if ($this->db->insert("customer", $data)) {
				//Now we can get the ID and update the newly created object
				$this->customer_code = $this->db->insert_id();
				return true;
			}
		}
		return false;
	}
}